﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class RetrieveStaffMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                displayStaffInformation();     
            }
        }

        protected void btnGoBackToMainMenu_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //go next page with queryString staffID
            Server.Transfer("UpdateStaff.aspx?StaffID=" + lblStaffID.Text);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            deleteStaffAndBankAccount();
        }

        private void deleteStaffAndBankAccount()
        {
            try
            {
                string deleteStaff = lblStaffID.Text;

                //Call DeleteBankAccount method
                int row2 = BankAccountStaffDBManager.deleteBankAccountStaff(deleteStaff);

                if (row2 > 0)
                {
                    //call DeleteStaff method
                    int row1 = StaffDBManager.deleteStaff(deleteStaff);
                    if (row1 > 0)
                    {
                        lblStatus.Text = "Staff ID: " + lblStaffID.Text + " has been successfully deleted";
                    }
                }
                else
                {
                    lblStatus.Text = "Delete unsuccessful. Please try again.";
                }
            }
            catch (Exception)
            {
                lblStatus.Text = "Sorry, This Staff cannot be deleted due to Foreign Key constraint. Please select another Staff";
            }
        }

        private void displayStaffInformation()
        {
            Staff s = StaffDBManager.getStaffById(Request.QueryString["StaffID"]);
            BankAccountStaff ba = BankAccountStaffDBManager.getBankAccountbyStaffId(Request.QueryString["StaffID"]);

            //Display General Information
            lblGetStaffName.Text = s.FirstName + " " + s.LastName;
            lblStaffID.Text = s.StaffID;
            lblAge.Text = s.Age.ToString();
            lblDateOfBirth.Text = s.DOB.ToString("d");
            lblGender.Text = s.Gender;
            lblDutyType.Text = s.DutyType;
            lblTitle.Text = s.Title;
            lblReportTo.Text = s.ReportTo;

            //Display Contact Information
            lblMobileNumber.Text = s.MobileNum.ToString();
            lblHomeNumber.Text = s.HomeNum.ToString();
            lblEmail.Text = s.Email;
            lblBlockNum.Text = s.BlockNum;
            lblStreetName.Text = s.StreetName;
            lblUnitNum.Text = s.UnitNum;
            lblPostalCode.Text = s.PostalCode;
            lblCountry.Text = s.Country;

            //Display Bank Account Information
            lblAccountName.Text = ba.AccountName;
            lblAccountType.Text = ba.AccountType;
            lblAccountNumber.Text = ba.AccountNumber;
            lblTransitCode.Text = ba.TransitCode;
            lblBankName.Text = ba.BankName;
            lblBankBranch.Text = ba.BankBranch;

            //Display Additional Information
            lblNotes.Text = s.Notes;
        }
    }
}